package com.lzq.framework.util.sms;

import com.lzq.framework.util.sms.sdkhttp.Client;
import com.lzq.framework.util.time.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import java.rmi.RemoteException;
import java.util.Date;

/**
 * @author 2019/6/5 15:05 by 刘赵强
 **/
@Slf4j
public class SmsUtils {
    /**
     * 发送短信  超爱财短信
     *
     * @param phone      手机号码
     * @param smsContent 短信内容
     * @throws RemoteException
     */
    public static void sendSms(String phone, String smsContent){
        int day = DateUtil.getDay(new Date());
        if (day % 2 == 0) {
            sendXBSms(phone, smsContent);
        } else {
            try {
                sendCACSms(phone, smsContent);
            } catch (RemoteException e) {
                log.error("sendCACSms发送短信失败,errorMsg={}",e.getMessage(),e);
            }
        }
    }

    /**
     * 动态调用web-service
     * 新博短信发送
     *
     * @param phone
     * @param smsContent
     */
    private static void sendXBSms(String phone, String smsContent) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = dcf.createClient("http://www.106551.com:81/ws/linkws.asmx?WSDL");
        Object[] objects;
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("BatchSend", "YX01263", "855172", phone, smsContent, "", "");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            log.error("发送短信异常errorMsg={}", e.getMessage(), e);
        } finally {
            if (null != client) {
                try {
                    client.close();
                } catch (Exception e) {
                    log.error("client关闭资源异常");
                }
            }
        }
    }

    /**
     * 超爱财发送短信
     *
     * @param phone
     * @param smsContent
     */
    private static void sendCACSms(String phone, String smsContent) throws RemoteException {
        //发送短信
        Client client = Client.getClient("http://sdk999ws.eucp.b2m.cn:8080/sdk/SDKService?wsdl");
        String softwareSerialNo = "9SDK-EMY-0999-JDVST";
        String key = "030234";
        if (null != client && StringUtils.isNotEmpty(softwareSerialNo) && StringUtils.isNotEmpty(key)) {
            client.sendSMS(softwareSerialNo, key, new String[]{phone}, smsContent, "", 5);
        } else {
            throw new RuntimeException("获取client失败");
        }
    }
}
